package com.hotpot.telecom.service.impl;

import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.hotpot.common.config.system.AsyncTaskService;
import com.hotpot.telecom.entity.BaseXid;
import com.hotpot.telecom.entity.TeleComGardenInfo;
import com.hotpot.telecom.entity.XidLocation;
import com.hotpot.telecom.mapper.TelecomMapper;
import com.hotpot.telecom.service.TeleComService;
import com.xiaoleilu.hutool.date.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 描述：
 *
 * @author: ruikanwang
 * @date: 2018/1/4 0004 10:42
 * @version: V1.0
 * 注意：本内容仅限于新疆感知科技有限公司内部传阅,禁止外泄以及用于其他的商业目
 * Copyright © 2018-ganinfo. All rights reserved.
 */
@Service
public class TeleComServiceImpl extends ServiceImpl<TelecomMapper,TeleComGardenInfo> implements TeleComService {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Autowired
    private AsyncTaskService asyncTaskService;
    /**
     * 获取所有数据
     */
    @Override
    public void getCusts(){
       List<TeleComGardenInfo> teleComGardenInfos= selectList(null);
       for (TeleComGardenInfo teleComGardenInfo : teleComGardenInfos){
           asyncTaskService.addXid(teleComGardenInfo);
       }
    }

    @Override
    public void getAllXidLocation() {
        Criteria criteria=Criteria.where("updateTime").lt(DateUtil.offsetDay(new Date(),-1).getTime());
        Query query = new Query();
        query.addCriteria(criteria);
        //查询总数
        long totalCount=mongoTemplate.count(query,BaseXid.class);
        //每页个数
        int numOfPage=1000;
        //计算总页数
        long totalPage=totalCount%numOfPage==0?(totalCount/numOfPage):(totalCount/numOfPage+1);
        for (int i=1;i<=totalPage;i++){
            int skip=(i-1)*numOfPage;
            query.skip(skip).limit(numOfPage);
            List<BaseXid> baseXids=mongoTemplate.find(query,BaseXid.class);
            for (BaseXid baseXid:baseXids){
                asyncTaskService.getHisLocation(baseXid,2);
            }
        }

    }
}
